﻿/*****************************************************************************
	SNEGA STG GAME ENGINE 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/sng-actionscript-stg-engine/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package com.sng.stg.game.event 
{
	import com.sng.stg.game.bullet.EnemyBullet;
	import com.sng.stg.game.enemy.Enemy;
	import com.sng.stg.game.StageViewer;
	import flash.display.BitmapData;
	
	/**
	 * ...
	 * @author snega
	 */
	public class ContinuousEnemyBulletGenerator extends ContinuousEvent
	{
		protected var _enemy:Enemy = null;
		
		protected var _bitmapData:BitmapData = null;
		protected var _pixelSnapping:String = "auto";
		protected var _smoothing:Boolean = false;
		
		protected var _duration:int, _offAngel:Number, _step:int, _speed:int, _radius:Number;
		protected var _angel:Number = 0;
		
		protected var _frame:int = 0;
		
		public function ContinuousEnemyBulletGenerator(enemy:Enemy, duration:int = 60, angel:int = 0, step:int = 0, speed:int = 0, radius:Number = 1, bitmapData:BitmapData = null, pixelSnapping:String = "auto", smoothing:Boolean = false) 
		{
			this._enemy = enemy;
			
			this._bitmapData = bitmapData;
			this._pixelSnapping = pixelSnapping;
			this._smoothing = smoothing;
			
			this._offAngel = angel * StageViewer.PI / 180;
			this._step = step;
			this._duration = duration;
			this._speed = speed;
			this._radius = radius;
		}
		
		override public function start(stage:StageViewer):void {
			this._stage = stage;
			
			this._frame = 0;
			
			// calculate angel
			this._angel = Math.atan((this._enemy.y - stage._player.y) / (this._enemy.x - stage._player.x));
			//trace(this._angel);
		}
		
		override public function process():void {
			if (++this._frame == this._duration) {
				this.remove();
			}
			
			// create bullet
			var bullet:EnemyBullet = new EnemyBullet(this._stage, this._radius, this._bitmapData, this._pixelSnapping, this._smoothing);
			bullet.x = this._enemy.x;
			bullet.y = this._enemy.y;
			bullet._vx = this._speed * Math.cos(this._angel);
			bullet._vy = this._speed * Math.sin(this._angel);
			bullet.rotation = this._angel;
			
			this._stage._enemyBulletList.push(bullet);
			this._stage._2dElement.addChild(bullet);
			
			// offset
			this._angel += this._offAngel;
		}
		
	}
	
}